﻿package javase.thread;

// Create a second thread.
class MyThread implements Runnable {
	Thread t;

	MyThread() {
		// Create a new, second thread
		t = new Thread(this, "Demo Thread");
		// t.setDaemon(true);
		System.out.println("Child thread: " + t);
		t.start(); // Start the thread
	}

	// This is the entry point for the second thread.
	public void run() {
		try {
			for (int i = 10; i > 0; i--) {
				System.out
						.println("------------------------Child Thread: " + i);
				Thread.sleep(2000);
			}

		} catch (InterruptedException e) {
			System.out.println("Child interrupted.");
		}

		System.out.println("Exiting child thread.");
	}
}

public class RunnableDemo {
	public static void main(String args[]) {
		MyThread mt = new MyThread(); // create a new thread
		try {
			for (int i = 10; i > 0; i--) {
				System.out.println("Main Thread: " + i);
				Thread.sleep(1000);
				//if(i==9)mt.t.join();
			}
		} catch (InterruptedException e) {
			System.out.println("Main thread interrupted.");
		}
		System.out.println("Main thread exiting.");
	}
}